package com.biostime.qdingding.modules.qd.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Courselevel;
import com.biostime.qdingding.modules.qd.entity.Courseserial;
import com.biostime.qdingding.modules.qd.entity.Coursetheme;
import com.biostime.qdingding.modules.qd.service.CourselevelService;
import com.biostime.qdingding.modules.qd.service.CourseserialService;
import com.biostime.qdingding.modules.qd.service.CoursethemeService;
import com.biostime.qdingding.modules.sys.utils.UserUtils;

/**
 * 课程主题Controller
 * @author king
 * @version 2015-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/coursetheme")
public class CoursethemeController extends BaseController {

	@Autowired
	private CoursethemeService coursethemeService;
	@Autowired
	private CourseserialService courseserialService;
	@Autowired
	private CourselevelService courselevelService;
	
	@ModelAttribute
	public Coursetheme get(@RequestParam(required=false) String id) {
		Coursetheme entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = coursethemeService.get(id);
		}
		if (entity == null){
			entity = new Coursetheme();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:coursetheme:view")
	@RequestMapping(value = {"list", ""})
	public String list(Coursetheme coursetheme, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Coursetheme> page = coursethemeService.findPage(new Page<Coursetheme>(request, response), coursetheme); 
		model.addAttribute("page", page);
		return "modules/qd/coursethemeList";
	}

	@RequiresPermissions("qd:coursetheme:view")
	@RequestMapping(value = "form")
	public String form(Coursetheme coursetheme, Model model) {
		
		List<Courseserial> serialList = courseserialService.findList(new Courseserial());
		List<Courselevel> levelList = courselevelService.findList(new Courselevel());
		
		model.addAttribute("levelList", levelList);
		model.addAttribute("serialList", serialList);
		model.addAttribute("coursetheme", coursetheme);
		return "modules/qd/coursethemeForm";
	}

	@RequiresPermissions(value={"qd:coursetheme:edit","qd:coursetheme:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Coursetheme coursetheme, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, coursetheme)){
			return form(coursetheme, model);
		}
		
		coursetheme.setCenter(UserUtils.getUser().getCenter());
		
		coursethemeService.save(coursetheme);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/coursetheme/?repage";
	}
	
	@RequiresPermissions("qd:coursetheme:edit")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Coursetheme coursetheme, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				coursethemeService.delete(new Coursetheme(id));
			}
		}else{
			coursethemeService.delete(coursetheme);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/coursetheme/?repage";
	}

}